package com.jsbs.iam.user.controller;
import com.github.pagehelper.PageInfo;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.StringUtils;
import com.jsbs.iam.user.constant.Constants;
import com.jsbs.iam.user.dto.ApplyTypeCodeDto;
import com.jsbs.iam.user.dto.IamApplyDto;
import com.jsbs.iam.user.dto.IamApplySystemDto;
import com.jsbs.iam.user.entity.IamApplySystem;
import com.jsbs.iam.user.service.IApplySystemService;
import com.jsbs.iam.user.service.UserInfoService;
import com.jsbs.iam.user.vo.IamApplyVo;
import com.jsbs.iam.user.vo.UserInfoVo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhouxiangyang
 * @className IamApplyController
 * @date 2022/12/15 19:47
 * @description: IAM应用中心Controller
 */
@RestController
@RequestMapping("/iam/user/Apply")
public class IamApplyController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(IamApplySystemController.class);

    @Autowired
    private IApplySystemService iApplySystemService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 获取应用列表
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getApplySystem")
    public Result getApplySystem (HttpServletRequest request) {
        try {
            // 请求头获取用户编码
//            String userCode = request.getHeader(Constants.USER_CODE);
            Cookie[] cookies = request.getCookies();
            String userCode = null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(Constants.USER_CODE)) {
                    userCode = cookie.getValue();
                }
            }
            if (StringUtils.isEmpty(userCode)) {
                return Result.failWithMsg("用户编码为空");
            }
            String companyCode = request.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                return Result.failWithMsg("归属租户为空");
            }
            String applyUrl = request.getHeader(Constants.APPLY_URL);
            if (StringUtils.isEmpty(applyUrl)) {
                return Result.failWithMsg("前端链接url为空");
            }
            List<IamApplySystem> applySystem = iApplySystemService.getApplySystem(userCode, companyCode,applyUrl);
            return Result.success(applySystem);
        } catch (Exception e) {
            return handleException(e, e.getMessage());
        }
    }

    /**
     * 登录策略分页查询
     * @param iamApplyDto
     * @return
     */
    @RequestMapping("/getIamApplyPage")
    public Result<PageInfo<IamApplyVo>> getIamApplyPage(@RequestBody IamApplyDto iamApplyDto,HttpServletRequest request) {
        String companyCode = request.getHeader(Constants.COMPANY_CODE);
        companyCode = "000001";
        if (StringUtils.isBlank(companyCode)) {
            throw new RuntimeException("当前归属租户状态异常！");
        }
        PageInfo<IamApplyVo> appTypeCodePage = iApplySystemService.getIamApplyPage(iamApplyDto,companyCode);
        return Result.success("登录策略分页查询成功",appTypeCodePage);
    }

    /**
     * 登录策略搜索成功  TODO：搜索接口并入分页查询接口 此接口是否需要保留
     * @param applyCodeOrName
     * @return
     */
    @RequestMapping("/getApplyCodeOrName")
    public Result<List<IamApplyVo>> getApplyCodeOrName(@RequestParam("applyCodeOrName") String applyCodeOrName) {
        List<IamApplyVo> IamApplyVoList = iApplySystemService.getApplyCodeOrName(applyCodeOrName);
        return Result.success("登录策略搜索成功",Result.success(IamApplyVoList));
    }

    /**
     * 应用系统下拉列表
     * @return
     */
    @RequestMapping("/getApplySystemAll")
    public Result<List<String>> getApplySystemAll() {
        List<String> applySystemAll = iApplySystemService.getApplySystemAll();
        if (CollectionUtils.isEmpty(applySystemAll)){
            return Result.fail("查询错误，未找到任何数据",null);
        }
        return Result.success("查询应用系统数据成功",applySystemAll);
    }

    @RequestMapping("/getApplyTypeCode")
    public Result getApplyTypeCode(@RequestBody IamApplySystemDto iamApplySystemDto) {
        String applyName = iamApplySystemDto.getApplyName();
        String applyTypeCode = iApplySystemService.getApplyTypeCode(applyName);
        return Result.success(applyTypeCode);
    }

    @RequestMapping("/getApplyTypeName")
    public Result getApplyTypeName(@RequestBody IamApplySystemDto iamApplySystemDto) {
        String applyCode = iamApplySystemDto.getApplyName();
        String applyTypeCode = iApplySystemService.getApplyTypeName(applyCode);
        return Result.success(applyTypeCode);
    }

    /**
     * 根据前端url获取应用类型编码
     * @param applyTypeCodeDto
     * @return
     */
    @RequestMapping("/queryApplyTypeCode")
    public Result queryApplyTypeCode(@RequestBody ApplyTypeCodeDto applyTypeCodeDto) {
        String applyTypeCode = iApplySystemService.queryApplyTypeCode(applyTypeCodeDto.getApplyUrl());
        return Result.success(applyTypeCode);
    }

    @RequestMapping("/getUserInfo")
    public Result<UserInfoVo> getUserInfo (HttpServletRequest request) {
        // 请求头获取用户编码
        String userCode = request.getHeader(Constants.USER_CODE);
        userCode="0000000001";
        if (StringUtils.isEmpty(userCode)) {
            return Result.failWithMsg("用户编码为空");
        }
        String companyCode = request.getHeader(Constants.COMPANY_CODE);
        companyCode = "000001";
        if (StringUtils.isEmpty(companyCode)) {
            return Result.failWithMsg("归属租户为空");
        }
        UserInfoVo userInfoVo = userInfoService.selectUserDetail(userCode,companyCode);
        return Result.success(userInfoVo);
    }

}
